/**
 *    @file       maintainthread.cpp
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/15/2014 06:57:41 PM
 *
 *    @author     Theophilus (), wangtf418@gmail.com 
 */
#include "maintainthread.h"
#include "server.h"
#include "logwrite.h"
#include "classfactory.h"
#define OUTPUT_STATE_INTERVAL 300
#define TIMER_TYPE_OUTPUT_STATE 0


maintainthread::maintainthread(bf_uint32_t type, bf_uint32_t size, bf_uint32_t index, const comargs *args)
	: netprocessthread(type, size, index, args)
{

}

maintainthread::~maintainthread()
{

}

bf_int_t maintainthread::init()
{
	bf_int_t ret = netprocessthread::init();
	if (ret != BF_OK)
	{
		return BF_ERROR;
	}
	baseconf_item &conf = server::get()->get_baseconf().get_conf();

	csocket *sock = new csocket(conf.m_maintain_ip, conf.m_maintain_port);
	sockettask *task = server::get()->get_classfactory()->gen_sockettask(TSK_TYPE_MAINTAIN_UDP, sock, this);
	if (!task)
	{
		delete sock;
	}
	ret = task->init(0);
	if (ret != BF_OK)
	{
		delete task;
		return BF_ERROR;
	}

	interval_timer itimer;
	itimer.m_timer_type = TIMER_TYPE_OUTPUT_STATE;
	itimer.m_interval = OUTPUT_STATE_INTERVAL;
	itimer.m_cyclenum = -1;
	add_timer(itimer);
	return BF_OK;
}


bf_int_t maintainthread::proc_msg(message *msg)
{
	msghead &head = msg->get_msghead();
	bf_int_t ret = BF_OK;
	switch (head.m_msgtype)
	{
		case MSG_TYPE_ITIMER_ONTIME:
			{
				simplemsgbody<bf_uint32_t> *body = (simplemsgbody<bf_uint32_t> *)msg->get_msgbody();
				if (!body || body->get_info() != TIMER_TYPE_OUTPUT_STATE)
				{
					ret = BF_ERROR;
					break;
				}
				output_state();
				break;
			}
		default:
			{
				ret = BF_ERROR;
				break;
			}
	}
	return BF_OK;
}

bf_int_t maintainthread::output_state()
{
	server::get()->write_messageq_state(LOGTYPE_SYS);
	return BF_OK;
}




